package com.example.firststory;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class Util {

	public static void textoPor(final TextView tv, final EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int count,
					int after) {

				tv.setText(String.valueOf(et.getText().length()));

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
	}

	public static ArrayList<String> getAllShownImagesPath(Activity activity) {

		Uri uri;
		Cursor cursor;
		int column_index_data, column_index_folder_name;
		ArrayList<String> listOfAllImages = new ArrayList<String>();
		String absolutePathOfImage = null;

		uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

		String[] projection = { MediaColumns.DATA,
				MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

		cursor = activity.getContentResolver().query(uri, projection, null,
				null, null);

		column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
		column_index_folder_name = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
		while (cursor.moveToNext()) {
			absolutePathOfImage = cursor.getString(column_index_data);

			listOfAllImages.add(absolutePathOfImage);
		}
		return listOfAllImages;
	}

	public static List<String> getAllFoldersWithImages(Activity activity) {

		ArrayList<String> listOfAllImages = new ArrayList<String>();
		List<String> listOfFolders = new ArrayList<String>();

		listOfAllImages = getAllShownImagesPath(activity);

		for (String file : listOfAllImages) {

			String folder = file.substring(0, file.lastIndexOf("/"));
			if (!listOfFolders.contains(folder)) {
				listOfFolders.add(folder);
				// Log.e("Imagen", file.substring(0, file.lastIndexOf("/")));
			}

		}

		return listOfFolders;

	}
}
